package com.my.number;

public class ConvertUtils {
	/**
	 * 4个字节16进制转换成float 浮点数
	 * 
	 * @return
	 */
	public static Float fourBytetoFloat(char[] c) {
		String binaryStr = "";
		String bin = "";
		for (int i = 0; i < 4; i++) {
			String str = Integer.toBinaryString(c[i]);
			bin = addZero(8 - str.length()) + str;
			binaryStr += bin;
		}


		int intBits = Integer.parseInt(binaryStr, 2);
		float myFloat = Float.intBitsToFloat(intBits);
		return myFloat;
	}

	/**
	 * 2个字节16进制转换成Int 整数
	 * 
	 * @return
	 */
	public static Integer twoBytetoInt(String data) {
		// "07D0"
		int parseInt = Integer.parseInt(data, 16);
		return parseInt;
	}

	public static String addZero(int num) {
		String res = "";
		while (num > 0) {
			res += "0";
			num--;
		}
		return res;
	}

	public static String byteToHex(byte b) {
		String hex = Integer.toHexString(b & 0xFF);
		if (hex.length() < 2) {
			hex = "0" + hex;
		}
		return hex;
	}

	// byte to char
	public static char hexStringToChar(String b) {
		return (char) Integer.parseInt(b, 16);
	}
}
